import 'package:flutter/material.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/widget/custom_widget/wrap_tag.dart';

class StoreCourseMenu extends StatefulWidget {
  final onTapF onTap;
  const StoreCourseMenu({Key key, this.onTap}) : super(key: key);

  @override
  _StoreCourseMenuState createState() => _StoreCourseMenuState();
}

class _StoreCourseMenuState extends State<StoreCourseMenu> {
  int firstIndex = 0, sectionIndex = 0;

  List firstLevels = new List<String>.generate(15, (int index) {
    return '$index区';
  });

  List secondLevels = new List<String>.generate(3, (int index) {
    return '全部';
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        buildLeft(context),
        Expanded(child: buildRight(context)),
      ],
    );
  }

  Widget buildLeft(BuildContext context) {
    return Container(
      width: Adapt.px(100),
      child: ListView.builder(
        itemCount: firstLevels.length,
        itemBuilder: (context, index) {
          return gestureDetector0(index, widget.onTap, firstLevels[index]);
        },
      ),
    );
  }

  Widget buildRight(BuildContext context) {
    return Padding(
        padding: EdgeInsets.symmetric(horizontal: Adapt.px(15)),
        child: CustomScrollView(
          slivers: secondLevels
              .map((e) => SliverList(
                    delegate: SliverChildListDelegate([
                      Container(
                          height: Adapt.px(30),
                          child: Text('深圳', style: TextStyles.titleStyle)),
                      GridView.builder(
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: 2,
                          itemBuilder: (c, i) {
                            return gestureDetector1(
                                i, widget.onTap, firstLevels, e);
                          },
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                            mainAxisSpacing: Adapt.px(10),
                            crossAxisSpacing: Adapt.px(10),
                            childAspectRatio: 120 / 36,
                            crossAxisCount: 2,
                          )),
                    ]),
                  ))
              .toList(),
        ));
  }

  GestureDetector gestureDetector0(
      int index, void itemOnTap(String selectValue), item) {
    return GestureDetector(
      onTap: () {
        firstIndex = index;
        setState(() {});
      },
      child: Container(
        height: Adapt.px(44),
        alignment: Alignment.center,
        color: firstIndex == index ? Colors.white : colorFAFAFA,
        child: firstIndex == index
            ? Text('$item', style: TextStyles.titleStyle)
            : Text('$item', style: TextStyles.hintTitleStyle),
      ),
    );
  }

  GestureDetector gestureDetector1(
      int index, void itemOnTap(String selectValue), List firstLevels, item) {
    return GestureDetector(
      onTap: () {
        sectionIndex = index;
        itemOnTap(item);
      },
      child: Container(
        height: Adapt.px(44),
        alignment: Alignment.center,
        color: colorFAFAFA,
        child: sectionIndex == index
            ? Text('$item', style: TextStyles.priceTitleStyle)
            : Text('$item', style: TextStyles.titleStyle),
      ),
    );
  }
}

class StoreCourseMenuGrid extends StatefulWidget {
  final onTapF onTap;

  // final
  StoreCourseMenuGrid({Key key, this.onTap}) : super(key: key);

  @override
  _StoreCourseMenuGridState createState() => _StoreCourseMenuGridState();
}

class _StoreCourseMenuGridState extends State<StoreCourseMenuGrid> {
  List list = new List<String>.generate(3, (int index) {
    return '全部';
  });

  int index;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: Adapt.px(15)),
      child: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          mainAxisSpacing: Adapt.px(10),
          crossAxisSpacing: Adapt.px(10),
          childAspectRatio: 120 / 36,
          crossAxisCount: 3,
        ),
        itemBuilder: (c, i) {
          return gestureDetector(i, widget.onTap, list[i]);
        },
        itemCount: list.length,
      ),
    );
  }

  GestureDetector gestureDetector(
      int index, void itemOnTap(String selectValue), item) {
    return GestureDetector(
      onTap: () {
        index = index;
        itemOnTap(item);
      },
      child: Container(
        height: Adapt.px(44),
        alignment: Alignment.center,
        color: colorFAFAFA,
        child: index == index
            ? Text('$item', style: TextStyles.priceTitleStyle)
            : Text('$item', style: TextStyles.titleStyle),
      ),
    );
  }
}
